<?php
/**
 * Created by PhpStorm.
 * User: this
 * Date: 2019/1/2
 * Time: 10:06
 */
namespace app\index\model;
use think\Db;
use think\Model;

class Product extends Model{
    /**
     * @param $condition
     * @param string $field
     * @param int $page
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getPage($condition,$field = '*' ,$page = 10){
        return $this->name('product')->field($field)->where($condition)->paginate($page);
    }

    /**
     * @param $condition
     * @param string $field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($condition,$field = '*'){
        return $this->name('product')->field($field)->where($condition)->find();
    }
    /**
     *
     * @param $condition
     * @param $field
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCate($condition,$field){
        return $this->name('product_category')->field($field)->where($condition)->cache(true,5)->select();
    }


    public function randSel($condition,$field = '*'){
        $where = '';
        if($condition){
            foreach ($condition as $k=>$v){
                $where .= " AND {$k}=$v";
            }
        }
        $res = Db::query("SELECT {$field} FROM bb_product WHERE product_id >=( SELECT floor( RAND() * (SELECT MAX(product_id) FROM bb_product))) {$where} ORDER BY product_id LIMIT 5");
        return $res;
    }
}